//
//  SingleChoose3ViewController.m
//  DebugDemo
//
//  Created by XFB on 2019/1/17.
//  Copyright © 2019年 ZhouCheng. All rights reserved.
//  失败的案例 因为会重用，主要使用于在cell少量的情况

#import "SingleChoose3ViewController.h"

#import "ChooseCell.h"
#import "ChooseModel.h"

@interface SingleChoose3ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;

// 是否选中
@property (nonatomic, assign) BOOL indexPathSelected;
// 上一次选择的索引
@property (nonatomic, assign) NSIndexPath *lastSelectIndexPath;

@end

@implementation SingleChoose3ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupBasicSet];
    [self setupUI];
}

- (void)setupBasicSet
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"单选3";
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemDidClick:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)setupUI
{
    self.indexPathSelected = NO;

    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
}

- (void)rightBarButtonItemDidClick:(UIBarButtonItem *)item
{
    if (self.lastSelectIndexPath) {

        ChooseModel *chooseModel = self.dataSource[self.lastSelectIndexPath.row];
        NSLog(@"---%@----", chooseModel.theme);
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"标题" message:chooseModel.theme preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alertVC addAction:okAction];
        [alertVC addAction:cancelAction];
        [self.navigationController presentViewController:alertVC animated:YES completion:nil];
    
    } else {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"标题" message:@"请选择一项" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alertVC addAction:okAction];
        [alertVC addAction:cancelAction];
        [self.navigationController presentViewController:alertVC animated:YES completion:nil];
    }
}

#pragma mark --- <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseCell"];
    cell.chooseModel2 = self.dataSource[indexPath.row];
    /// 最好不要用这个来判断，因为会重用
    if (self.indexPathSelected) {
        cell.isSelected = YES;
    } else {
        cell.isSelected = NO;
    }
    return cell;
}

#pragma mark --- <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /// 暂存上一次选中的行
    NSIndexPath *tempIndexPath = self.lastSelectIndexPath;
    /// 如果上一次选中的行存在，并且不是当前选中的行，则让上一行不选中
    if (tempIndexPath && tempIndexPath != indexPath) {
        /// 修改之前选中cell数据为不选中
        self.indexPathSelected = NO;
        [tableView reloadRowsAtIndexPaths:@[tempIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    /// 选中的修改为当期行
    self.lastSelectIndexPath = indexPath;
    /// 修改这个被选中的行
    self.indexPathSelected = YES;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-NavBarHeight-BottomHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerNib:[UINib nibWithNibName:@"ChooseCell" bundle:nil] forCellReuseIdentifier:@"ChooseCell"];
    }
    return _tableView;
}

- (NSArray *)dataSource
{
    if (_dataSource == nil) {
        NSArray *plistArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ChooseModel" ofType:@"plist"]];
        NSArray *modelArray = [ChooseModel mj_objectArrayWithKeyValuesArray:plistArray];
        _dataSource = [modelArray mutableCopy];
    }
    return _dataSource;
}

@end
